package com.base.llpark;

import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.widget.SingleChooseDialog;
import com.base.llpark.adapter.BaseAdapter;
import com.base.llpark.adapter.BaseHolder;
import com.base.llpark.bean.IllegalPark;
import com.base.llpark.bean.LlParkBean;
import com.base.llpark.databinding.ActivityParksearchBinding;
import com.base.llpark.utills.GlideUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by arvin on 2018/11/7.
 */
@Route(path = "/lpark/parksearch")
public class IllparkSearchActivity extends CBaseBarMvpActivity implements View.OnClickListener {
    protected CompositeDisposable mCompositeDisposable=new CompositeDisposable();

    private ActivityParksearchBinding mBinding;
    public String[] mShengs = {"苏", "京", "津", "沪", "渝", "豫", "云", "辽", "黑", "浙", "皖", "湘", "赣", "新", "鄂", "贵", "甘", "晋", "蒙", "陕", "吉", "闽", "粤", "青", "藏", "川", "宁", "琼", "桂", "鲁", "冀"};
    public String[] mLetters = {"E", "B", "C", "D", "A", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    String pattern = "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{5})|([A-HJ-Z]{1}(([DF]{1}[A-HJ-NP-Z0-9]{1}[0-9]{4})|([0-9]{5}[DF]{1})))|([A-HJ-Z]{1}[A-D0-9]{1}[0-9]{3}警)))|([0-9]{6}使)|((([沪粤川云桂鄂陕蒙藏黑辽渝]{1}A)|鲁B|闽D|蒙E|蒙H)[0-9]{4}领)|(WJ[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼·•]{1}[0-9]{4}[TDSHBXJ0-9]{1})|([VKHBSLJNGCE]{1}[A-DJ-PR-TVY]{1}[0-9]{5})";
    private List<IllegalPark> mData=new ArrayList<>();
    private BaseAdapter<IllegalPark> mAdapter;


    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityParksearchBinding) mRootBinding;
        setBackNavigation();
        setBarTitle("违停查询");
        mBinding.ivSearch.setOnClickListener(this);
        mBinding.tvSheng.setOnClickListener(this);
        mBinding.tvLetter.setOnClickListener(this);
        mAdapter = new BaseAdapter<IllegalPark>(mData, mContext) {

            @Override
            public void convert(BaseHolder holder, IllegalPark park, boolean isHeader, boolean isBottom) {
                ImageView iv = holder.getView(R.id.iv);
                TextView num = holder.getView(R.id.num);
                TextView address = holder.getView(R.id.address);
                TextView date = holder.getView(R.id.date);
                ImageView iv_state = holder.getView(R.id.state);
                if (park.getStateName() != null && park.getStateName().equals("未处理")) {
                    GlideUtil.loadResource(mContext, R.drawable.wcl, iv_state);

                } else if (park.getStateName() != null && park.getStateName().equals("罚款处理") || park.getStateName() != null && park.getStateName().equals("销案处理") || park.getStateName() != null && park.getStateName().equals("教育处理")) {
                    GlideUtil.loadResource(mContext, R.drawable.ycl, iv_state);

                } else if (park.getStateName() != null && park.getStateName().equals("移交公安")) {
                    GlideUtil.loadResource(mContext, R.drawable.yyj, iv_state);
                } else if (park.getStateName() != null && park.getStateName().equals("转入处罚")) {
                    GlideUtil.loadResource(mContext, R.drawable.zhuruchufa, iv_state);
                }
                GlideUtil.load(mContext, RealNetWorkUtil.getBaseUrl()+"/api-file/"+park.getFileAddress(), iv);
                num.setText(park.getCarNo());
                address.setText(park.getCheckSignAddressName());
                date.setText(park.getCheckDate());

            }
        };
        mAdapter.putNormalType(R.layout.item_park);
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                IllegalPark illegalPark = mData.get(dataPosition);
                ARouter.getInstance().build("/lpark/parkdetail").withParcelable("mPark",illegalPark).navigation();
            }
        });
        mBinding.rv.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rv.setAdapter(mAdapter);
    }

    @Override
    protected void start() {

    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_parksearch;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.iv_search) {
            if (TextUtils.isEmpty(mBinding.etNum.getText().toString().trim())) {
                return;
            }
            String number = mBinding.tvSheng.getText().toString() + mBinding.tvLetter.getText().toString() + mBinding.etNum.getText().toString();
            number = number.toUpperCase();
            if (Pattern.matches(pattern, number)) {
                search(number);
            } else {
                showMes("车牌格式不对");
            }


        } else if (i == R.id.tv_sheng) {
            List<String> strs_sheng = Arrays.asList(mShengs);
            new SingleChooseDialog<String>(mContext, "请选择省份", strs_sheng, new SingleChooseDialog.OnItemChooseListener() {

                @Override
                public void onItemChoose(Object o) {
                    mBinding.tvSheng.setText(o.toString());
                }
            }).show();

        } else if (i == R.id.tv_letter) {
            List<String> strs_letter = Arrays.asList(mLetters);
            new SingleChooseDialog<String>(mContext, "请选择首字母", strs_letter, new SingleChooseDialog.OnItemChooseListener() {

                @Override
                public void onItemChoose(Object o) {
                    mBinding.tvLetter.setText(o.toString());
                }
            }).show();
        }
    }

    private void search(String num)
    {
        showPb();
        HashMap<String, Object> map = new HashMap<>();
        map.put("carNo",num);
        map.put("page",1);
        map.put("limit",100);
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-ll/illegal/phoneQuery").content(map)
                .build().enqueue(LlParkBean.class, new CallBackImpl<LlParkBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        hidePb();
                        showError();
                    }

                    @Override
                    public void onNext(LlParkBean list) {
                        hidePb();
                        mData.clear();
                        mData.addAll(list.getRows());
                        mAdapter.notifyDataSetChanged();
                        if(mData.size()==0)
                        {
                            mBinding.empty.setVisibility(View.VISIBLE);
                        }else {
                            mBinding.empty.setVisibility(View.GONE);
                        }
                    }
                });

        mCompositeDisposable.add(enqueue);
    }
}
